package com.example.pagergallery

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.navigation.findNavController
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import io.supercharge.shimmerlayout.ShimmerLayout

class GalleryAdapter:ListAdapter<PhotoItem,MyViewHolder>(DIFFCALLBACK) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val holder = MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.galley_cell,parent,false))
        holder.itemView.setOnClickListener {
            Bundle().apply {
                putParcelableArrayList("PHOTO_LIST",ArrayList(currentList))   //创建一个ArrayList放入Bundle
                putInt("PHOTO_POSITION",holder.adapterPosition)   //当前你点击的是第几个图片  将当前点中的图片在适配器中的位置传递过去
                holder.itemView.findNavController().navigate(R.id.action_galleryFragment_to_pagerPhotoFragment,this)
            }
        }

        return holder
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val photoItem = getItem(position)
        val shimmerLayout = holder.itemView.findViewById<ShimmerLayout>(R.id.shimmerLayoutCell)
        with(holder.itemView){
            shimmerLayout.apply {
                setShimmerColor(0x55FFFFFF)
                setShimmerAngle(0)
                startShimmerAnimation()
            }
            holder.itemView.findViewById<TextView>(R.id.textViewUser).text = photoItem.photoUser
            holder.itemView.findViewById<TextView>(R.id.textViewViews).text = photoItem.photoViews.toString()
            holder.itemView.findViewById<TextView>(R.id.textViewLikes).text = photoItem.photoLikes.toString()
            holder.itemView.findViewById<TextView>(R.id.textViewId).text = photoItem.photoId.toString()
            holder.itemView.findViewById<ImageView>(R.id.imageView).layoutParams.height = photoItem.photoHeight   //取回当前图片对象以及图片高度
        }

        Glide.with(holder.itemView)
            .load(getItem(position).previewUrl)
            .placeholder(R.drawable.photo_placeholder)
            .listener(object :RequestListener<Drawable>{
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false.also { holder.itemView.findViewById<ShimmerLayout>(R.id.shimmerLayoutCell)?.stopShimmerAnimation() }
                }

            })
            .into(holder.itemView.findViewById(R.id.imageView))
    }



    object DIFFCALLBACK:DiffUtil.ItemCallback<PhotoItem>() {
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem === newItem
        }

        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.photoId == newItem.photoId
        }
    }


}

class MyViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView)